import NewWebSocket from '../libs/utils/websocket'
export default {
    data() {
        return {
            socket: new NewWebSocket("ws://localhost:9000", {}, this.handleMessage),
            socketData: ''
        }
    },
    created() {
    },
    methods: {
        // 重新开启socket
        startSocket() {
            if(this.socket.socket) return
            this.socket.init()   
        },
        // 暂停socket
        closeSocket() {
            this.socket.close()
        },
        // 发送信息给后端
        socketSend(data) {
            this.socket.sendMessage(data)
        },
        // 接受服务端返回的数据
        handleMessage(data) {
            console.log('查看返回数据:', data)
            this.socketData = data
        },
    },
    beforeDestroy() {
        this.socket.close()
        this.socket = null
    }
}